package com.pantech.app.call_log_enabler;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

public class IconService extends Service {
	
	private static final String LOG_TAG = "LogApp";
	private NotificationManager notificationManager;
	protected boolean mRunning;
	private String text;
	private int icon;
	
	private UserSharedPreference mUserSharedPreference;

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
		//unregister alarm
		Intent intent = new Intent(IconService.this, SwitchReceiver.class);
		intent.setAction(SwitchReceiver.ACTION_RESTART_ICONSERVICE);
		PendingIntent sender = PendingIntent.getBroadcast(IconService.this, 0, intent, 0);
		AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
	    am.cancel(sender);
	    
		notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		mUserSharedPreference = new UserSharedPreference(getApplicationContext());
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.e(LOG_TAG, "Make log IconService onDestroy()");
		mRunning = false;
		notificationManager.cancel(0);
		
		if(mUserSharedPreference.getIsCallLog_ongoing() || MainActivity.isLogEnabled) {
			//register alram
			Intent intent = new Intent(IconService.this, SwitchReceiver.class);
			intent.setAction(SwitchReceiver.ACTION_RESTART_ICONSERVICE);
			intent.putExtra("text", text);
	    	intent.putExtra("icon", icon);
			PendingIntent sender = PendingIntent.getBroadcast(IconService.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
			long firstTime = SystemClock.elapsedRealtime();
			firstTime += 0.5*1000;
			AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
			am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 5*1000, sender);
		}
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.e(LOG_TAG, "Make log IconService run()");
		if(intent != null) {
			text = intent.getStringExtra("text");
			icon = intent.getIntExtra("icon", R.drawable.emo_im_laughing);
//			new Thread(new Runnable() {
//				@Override
//				public void run() {
//					mRunning = true;
//					while(mRunning) {
						mHandler.sendEmptyMessage(0);
//						SystemClock.sleep(1000 * 1000);
//					}
//				}
//			}).start();
		}
		return super.onStartCommand(intent, flags, startId);
	}
	
	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), 0,
	    			new Intent(getApplicationContext(), MainActivity.class)
	    					.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT), 0);
	    	
			NotificationCompat.Builder builder = new NotificationCompat.Builder(IconService.this);
			builder.setContentTitle("Call Log is enabled")
					.setContentText(text)
	    			.setContentIntent(intent)
	    			.setSmallIcon(icon)
	    			.setAutoCancel(false);
			Notification noti = builder.build();
			noti.flags |= Notification.FLAG_NO_CLEAR;
	    	
	    	notificationManager.notify(0, noti);
		};
	};

}
